//
//  TZRommendHeader.m
//  XMLYFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "TZRommendHeader.h"
#import <UIImageView+WebCache.h>
#import <Masonry.h>
@interface TZRommendHeader()<UIScrollViewDelegate>
@property(nonatomic,retain)NSTimer *myTimer;
@end


@implementation TZRommendHeader

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.ImageArr = [NSMutableArray array];
    
    }
    return self;
}

-(void)creatScollView:(NSMutableArray *)array
{
    array = self.ImageArr;
    self.headScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, TZWIDTH, 220)];
    self.headScrollView.backgroundColor = [UIColor colorWithRed:0.654 green:1.000 blue:0.948 alpha:1.000];
    self.headScrollView.contentSize = CGSizeMake(TZWIDTH*array.count, 220);
    self.headScrollView.delegate = self;
    self.headScrollView.contentOffset = CGPointMake(TZWIDTH, 0);
    self.headScrollView.showsHorizontalScrollIndicator = NO;
    self.headScrollView.pagingEnabled = YES;
    [self addSubview: self.headScrollView];
    self.headScrollView.contentOffset = CGPointMake(TZWIDTH, 0);
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];

}


-(void)creatImage:(NSMutableArray *)array
{

    for (int i = 0; i < array.count; i++) {
        UIImageView *myimage = [[UIImageView alloc]initWithFrame:CGRectMake(TZWIDTH*i, 0, TZWIDTH, 220)];
        myimage.backgroundColor = [UIColor colorWithRed:1.000 green:0.512 blue:0.762 alpha:1.000];
        myimage.userInteractionEnabled = YES;
        myimage.tag = 1000+i;
        [myimage sd_setImageWithURL:[array objectAtIndex:i] placeholderImage:nil];
        [self.headScrollView addSubview:myimage];

    }
}



-(void)setDic:(NSMutableDictionary *)dic
{

    NSMutableArray *array = dic[@"list"];
    NSString *first = [array lastObject][@"pic"];
    [self.ImageArr addObject:first];

    NSString *last = [array firstObject][@"pic"];
    for (NSMutableDictionary *dic in array) {
        NSString *str = dic[@"pic"];
        [self.ImageArr addObject:str];
        
    }
    [self.ImageArr addObject:last];
    
    [self creatScollView:self.ImageArr];
    
    [self creatImage:self.ImageArr];
    
}

//轮播图翻页移动方法

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
  
    if (self.headScrollView.contentOffset.x == TZWIDTH*(self.ImageArr.count-1)) {
        self.headScrollView.contentOffset = CGPointMake(TZWIDTH, 0);
        
    }
    if (self.headScrollView.contentOffset.x == 0) {
        self.headScrollView.contentOffset = CGPointMake(TZWIDTH*(self.ImageArr.count - 2), 0);
        
        
    }
    
}


-(void)timerAction:(NSTimer *)timer
{
    
    //使用scrollview自带的滚动动画,实现起来效果不好,有bug.所以需要自己写动画效果
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.headScrollView setContentOffset:CGPointMake((self.headScrollView.contentOffset.x/TZWIDTH + 1) * self.headScrollView.frame.size.width, 0)];
        
    }];
    //如果滑动到最后一页
    if (self.headScrollView.contentOffset.x == (self.ImageArr.count-1)*self.headScrollView.frame.size.width) {
        //快速移动到第一页
        [self.headScrollView setContentOffset:CGPointMake(self.headScrollView.frame.size.width, 0)];
        
        
    }
    
    
}


@end
